/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fyx.secure.web;

import java.util.Observable;
import java.util.Observer;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import org.fyx.mula.ejb.MulaLocal;
import org.fyx.core.web.event.MenuCreatingEventHandler;
import org.fyx.core.web.event.RoleCreatingEventHandler;
import org.fyx.linker.web.LinkerLocal;
import org.fyx.secure.entity.SecureRole;
import org.fyx.secure.ejb.SecureLocal;
import org.fyx.secure.ejb.SecureRoles;

/**
 *
 * @author xljan
 */
@Stateless
@LocalBean
public class SecureObserver implements Observer {

    @EJB
    private SecureLocal secure;
    @EJB
    private LinkerLocal linker;
    @EJB
    private MulaLocal mula;

    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof MenuCreatingEventHandler) {
            menuCreate();
        } else if (o instanceof RoleCreatingEventHandler) {
            RoleCreate();
        }
    }

    private void menuCreate() {
        linker.addMenuItem(mula.get("secure_users"),
                linker.getLink("secure/user/list.xhtml"),
                "secure_users", "ui-icon ui-icon-person",
                SecureRoles.USER_MANAGER);

        linker.addMenuItem(mula.get("secure_groups"),
                linker.getLink("secure/group/list.xhtml"),
                "secure_groups", "ui-icon ui-icon-note",
                SecureRoles.GROUP_MANAGER);
    }

    private void RoleCreate() {
        SecureRole userManager = new SecureRole();
        userManager.setCode(SecureRoles.USER_MANAGER);
        userManager.setTitle("User manager");
        userManager.setDescription("Can show users details.");
        userManager.setCategory("Security");
        secure.addRole(userManager);

        SecureRole groupManager = new SecureRole();
        groupManager.setCode(SecureRoles.GROUP_MANAGER);
        groupManager.setTitle("Group manager");
        groupManager.setDescription("Can managed user's groups.");
        groupManager.setCategory("Security");
        secure.addRole(groupManager);

        SecureRole userEditManager = new SecureRole();
        userEditManager.setCode(SecureRoles.USER_EDIT_MANAGER);
        userEditManager.setTitle("User edit manager");
        userEditManager.setDescription("Can edit and delete users.");
        userEditManager.setCategory("Security");
        secure.addRole(userEditManager);
    }
}
